package io.awen.modules.sign.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.awen.common.utils.DateUtils;
import io.awen.common.utils.PageUtils;
import io.awen.common.utils.Query;
import io.awen.modules.sign.StatusConstants;
import io.awen.modules.sign.dao.SignContainerAlarmDao;
import io.awen.modules.sign.entity.SignContainerAlarmEntity;
import io.awen.modules.sign.entity.SignContainerEntity;
import io.awen.modules.sign.qo.SignContainerAlarmQo;
import io.awen.modules.sign.service.SignCarDispatchService;
import io.awen.modules.sign.service.SignContainerAlarmService;
import io.awen.modules.sign.service.SignContainerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;


@Service("signContainerAlarmService")
public class SignContainerAlarmServiceImpl extends ServiceImpl<SignContainerAlarmDao, SignContainerAlarmEntity> implements SignContainerAlarmService {

    @Autowired
    private SignCarDispatchService signCarDispatchService;

    @Autowired
    private SignContainerService signContainerService;

    @Override
    public PageUtils queryPage(SignContainerAlarmQo qo) {
        IPage<SignContainerAlarmEntity> page = this.page(
                new Query<SignContainerAlarmEntity>().getPage(qo),
                new QueryWrapper<SignContainerAlarmEntity>().lambda()
                        .eq(qo.getContainerId() != null, SignContainerAlarmEntity::getContainerId, qo.getContainerId())
                        .eq(qo.getCtUnitId() != null, SignContainerAlarmEntity::getCtUnitId, qo.getCtUnitId())
                        .ge(qo.getStartTime() != null, SignContainerAlarmEntity::getCreateTime, qo.getStartTime())
                        .le(qo.getEndTime() != null, SignContainerAlarmEntity::getCreateTime, qo.getEndTime())
                        .eq(StringUtils.isNotBlank(qo.getStatus()), SignContainerAlarmEntity::getStatus, qo.getStatus())
                        .orderByDesc(SignContainerAlarmEntity::getCreateTime)
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageOfNoFinsih(SignContainerAlarmQo qo) {
        IPage<SignContainerAlarmEntity> page = this.page(
                new Query<SignContainerAlarmEntity>().getPage(qo),
                new QueryWrapper<SignContainerAlarmEntity>().lambda()
                        .ne(StringUtils.isNotBlank(qo.getStatus()), SignContainerAlarmEntity::getStatus, qo.getStatus())
                        .orderByDesc(SignContainerAlarmEntity::getCreateTime)
        );

        for(SignContainerAlarmEntity e:page.getRecords()){
            Long containerId = e.getContainerId();
            SignContainerEntity byId = signContainerService.getById(containerId);
            if(byId!=null){
                e.setContainerWarningLiquidValue(byId.getContainerWarningLiquidValue());
            }
        }
        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void juegeAlarm(SignContainerEntity container) {
        // 判断是否报警
        if (container.getSensorValue() < container.getContainerWarningLiquidValue()) {
            return;
        }

        List<String> statusList = new ArrayList<>(2);
        statusList.add(StatusConstants.ALARM_STATUS_WAITED);
        statusList.add(StatusConstants.ALARM_STATUS_PROCESSING);
        List<SignContainerAlarmEntity> alarmList = list(
                new QueryWrapper<SignContainerAlarmEntity>().lambda()
                        .eq(SignContainerAlarmEntity::getContainerId, container.getId())
                        .in(SignContainerAlarmEntity::getStatus, statusList)
        );

        if (alarmList == null || alarmList.size() == 0) {
            SignContainerAlarmEntity alarm = new SignContainerAlarmEntity();
            alarm.setContainerId(container.getId());
            alarm.setCtUnitId(container.getCtUnitId());
            alarm.setAlarmLiquidValue(container.getSensorValue());
            alarm.setStatus(StatusConstants.ALARM_STATUS_PROCESSING);
            alarm.setAlarmTime(new Date());
            alarm.setAlarmAddress(container.getContainerAddress());
            alarm.setName(container.getName() + "-预警[" + DateUtils.format(alarm.getAlarmTime(), "yyyy年MM月dd日HH时mm分") + "]");

            save(alarm);

            signCarDispatchService.autoDispatch(container, alarm);
        }
    }

    @Override
    public int total(SignContainerAlarmQo qo) {
        int count = this.count(
                new QueryWrapper<SignContainerAlarmEntity>().lambda()
                        .eq(qo.getCtUnitId() != null, SignContainerAlarmEntity::getCtUnitId, qo.getCtUnitId())
                        .ge(qo.getStartTime() != null, SignContainerAlarmEntity::getCreateTime, qo.getStartTime())
                        .le(qo.getEndTime() != null, SignContainerAlarmEntity::getCreateTime, qo.getEndTime())
                        .eq(StringUtils.isNotBlank(qo.getStatus()), SignContainerAlarmEntity::getStatus, qo.getStatus())
        );
        return count;
    }

}